package cn.xuan.scan.dao.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Vector;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Expression;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import cn.xuan.scan.dao.CommonPersistDAO;
import cn.xuan.scan.dao.query.param.CommonPersistContext;

@Component
public class CommonPersistDAOImpl extends HibernateDaoSupport implements CommonPersistDAO{
	

	private CommonPersistContext commonPersistContext;
	
	@Resource(name = "sessionFactory")
	private void setMySessionFactory(SessionFactory sessionFactory) {
		// 这个方法名可以随便写，@Resource可以通过name 或者type来装载的。
		super.setSessionFactory(sessionFactory);
	}

	public Vector createAll(Vector _obj) {
		if (_obj != null && _obj.size() != 0) {
			this.saveOrUpdateAll(_obj);
			
			return _obj;
		} else {
			return _obj;
		}
	}

	public Object createObject(Object _obj) {
		this.create(_obj);
		return _obj;
	}

	public void deleteAll(Vector _obj) {
		if (_obj != null && _obj.size() != 0) {
			this.deleteAll(_obj);
		}
	}

	public void deleteObject(Object _obj) {
		this.delete(_obj);
	}

	public Vector updateAll(Vector _obj) {
		if (_obj != null && _obj.size() != 0) {
			this.saveOrUpdateAll(_obj);
			return _obj;
		} else {
			return _obj;
		}
	}

	public Object updateObject(Object _obj) {
		this.saveOrUpdate(_obj);
		return _obj;
	}

	public Object mergeObject(Object _obj) {
		this.merge(_obj);
		return _obj;
	}

	private String getSimplyClassName(Object _obj) {
		String className = _obj.getClass().getName();
		if (className.lastIndexOf(".") >= 0) {
			className = className.substring(className.lastIndexOf(".") + 1, className.length());
		}

		return className;
	}

	public CommonPersistContext getCommonPersistContext() {
		return this.commonPersistContext;
	}

	public void setCommonPersistContext(CommonPersistContext _commonPersistContext) {
		this.commonPersistContext = _commonPersistContext;
	}

	
	public void create(Object entity) {
		this.getHibernateTemplate().save(entity);
	}

	public void save(Object entity) {
		this.getHibernateTemplate().save(entity);
	}

	public void saveOrUpdate(Object entity) {
		this.getHibernateTemplate().saveOrUpdate(entity);
	}

	public void update(Object entity) {
		this.getHibernateTemplate().update(entity);
	}

	public void merge(Object entity) {
		this.getHibernateTemplate().merge(entity);
	}

	public void delete(Object entity) {
		this.getHibernateTemplate().delete(entity);
	}

	public void saveOrUpdateAll(Collection entity) {
		for (Object object : entity) {
			this.getHibernateTemplate().save(entity);
		}
//		this.getHibernateTemplate().saveOrUpdateAll(entity);
	}

	public void deleteAll(Collection entity) {
		this.getHibernateTemplate().deleteAll(entity);
	}

	public void deleteById(Class entity, Serializable id) {
		this.getHibernateTemplate().delete(this.getHibernateTemplate().load(entity, id));
	}

	public Object loadById(Class entity, Serializable id) {
		return this.getHibernateTemplate().load(entity, id);
	}

	public Object findById(Class entity, Serializable id) {
		Session session = null;

		Object var7;
		try {
			session = this.getSessionFactory().openSession(); // this.getSession(false);
			List result = null;
			Criteria criteria = session.createCriteria(entity);
			criteria.add(Expression.idEq(id));
			result = criteria.list();
			if (result == null || result.size() <= 0) {
				return null;
			}

			var7 = result.get(0);
		} catch (Exception var10) {
			var10.printStackTrace();
			return null;
		} finally {
			SessionFactoryUtils.closeSession(session);//this.releaseSession(session);
		}

		return var7;
	}

	public List findAll(Class entity) {
		Session session = null;

		try {
			session = this.getSessionFactory().openSession(); // this.getSession(false);
			List result = null;
			Criteria criteria = session.createCriteria(entity);
			result = criteria.list();
			List var6 = result;
			return var6;
		} catch (Exception var9) {
			var9.printStackTrace();
		} finally {
			SessionFactoryUtils.closeSession(session);//this.releaseSession(session);
		}

		return null;
	}

	public List find(String queryHQL) {
		Session session = null;

		try {
			session = this.getSessionFactory().openSession(); // this.getSession(false);
			List result = null;
			Query query = session.createQuery(queryHQL);
			result = query.list();
			List var6 = result;
			return var6;
		} catch (Exception var9) {
			var9.printStackTrace();
		} finally {
			SessionFactoryUtils.closeSession(session);//this.releaseSession(session);
		}

		return null;
	}
}
